package search.impl;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import search.FileHandler;
import vo.Program;

public class FileHandlerImpl implements FileHandler{
	
    public int program2File( List<Program> programList) {
    	boolean flag;
    	int cnt = 0;
    	for(Program p:programList) {
    		flag = program2File(p);
    		if(flag == true) {
    			cnt++;
    		}
    	}
    	return cnt;
    }
    
   private boolean program2File(Program p) {
			try {
				FileOutputStream file_fi = new FileOutputStream("C:\\Users\\胡伟\\Desktop\\Java-WorkSpace\\java-example-4\\data.txt",true);
				OutputStreamWriter file_sr = new OutputStreamWriter(file_fi, "UTF-8");
				BufferedWriter file = new BufferedWriter(file_sr);
				//由于可能开设多个学位要分条写入所以在此判断
				for(int i=0;i<p.getDegree().split("\t").length;i++)
				{
					String finalDDLWithAid;
					String finalDDLWithoutAid;
					
					String finalDegree = p.getDegree().split("\t")[i].trim();
					if(finalDegree.isEmpty()) {
						finalDegree = "NULL";
					}
					if(p.getDeadlineWithAid()=="NULL"&&p.getDegree().split("\t").length>1) {
						finalDDLWithAid = "NULL";
					}else {
						finalDDLWithAid = p.getDeadlineWithAid().split("\t")[i].trim();
						if(finalDDLWithAid.isEmpty()) {
							finalDDLWithAid = "NULL";
						}
					}	
					if(p.getDeadlineWithoutAid()=="NULL"&&p.getDegree().split("\t").length>1) {
						finalDDLWithoutAid = "NULL";
					}else {
						finalDDLWithoutAid = p.getDeadlineWithAid().split("\t")[i].trim();
						if(finalDDLWithoutAid.isEmpty()) {
							finalDDLWithoutAid = "NULL";
						}
					}

					//testcode -- 0
//					System.out.println("Country: "+p.getCountry());
//					System.out.println("Email: "+p.getEmail());
//					System.out.println("Homepage: "+p.getHomepage());
//					System.out.println("Id: "+p.getId());
//					System.out.println("Location: "+p.getLocation());
//					System.out.println("PhoneNumber: "+p.getPhoneNumber());
//					System.out.println("ProgramName: "+p.getProgramName());
//					System.out.println("School: "+p.getSchool());
//					System.out.println("University: "+p.getUniversity());
//					System.out.println("DeadlineWithAid: "+p.getDeadlineWithAid());
//					System.out.println("DeadlineWithoutAid: "+p.getDeadlineWithoutAid());
//					System.out.println("Degree: "+p.getDegree());
					//testcode -- 1
					file.write(p.getUniversity()); file.write("\t");
					file.write(p.getCountry()); file.write("\t");
					file.write(p.getProgramName()); file.write("\t");
					file.write(p.getSchool()); file.write("\t");
					file.write(finalDegree); file.write("\t");
					file.write(p.getEmail()); file.write("\t");
					file.write(p.getPhoneNumber()); file.write("\t");
					file.write(p.getLocation()); file.write("\t");
					file.write(finalDDLWithAid); file.write("\t");
					file.write(finalDDLWithoutAid); file.write("\t");
					file.write(p.getHomepage()); file.write("\t");
					file.newLine();
				}
			file.close();
			file_sr.close();
			file_fi.close();
			return true;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
	}
}
